﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow.forecast
{
    public partial class Europe : System.Web.UI.Page
    {
        #region 页面加载事件
        protected string Name,Product="预报产品",Href="index.aspx";
        protected string type = "EC_ThinFore"; //传过来的表名
        protected bool isOptimize; //是否是优化
        protected string OptimalURL="index.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            string TQZX_Editor = ConfigurationManager.AppSettings["tqzxfore"].ToString();
            string[] editors = TQZX_Editor.Split(',');
            bool is_editor = false;
            if (editors.Contains(Session["nowuser"])) 
            {
                is_editor = true;
            }
            type = Request.QueryString["type"];
            Name = Request.QueryString["name"];
            if (type.ToLower() == "TQZXFore".ToLower() && (Convert.ToBoolean(Session["isadmin"])==true || is_editor==true))
            {
                btn_add.Visible = true;
            }
            else
            {
                btn_add.Visible = false;
            }
            if (string.IsNullOrEmpty(type))
            {
                Response.Redirect("Index.aspx");
                Response.End();
            }
            if (string.IsNullOrEmpty(Request.QueryString["optimize"]))
            {
                isOptimize = false;
            }
            else
            {
                isOptimize = true;
                Product = "预报优化";
                Href = "IndexOptimize.aspx";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["opt"]))
            {
                Href = "IndexOptimize.aspx";
            }
            this.anp.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"]);
            if (!IsPostBack)
            {
                time_begin.Text = DateTime.Now.AddDays(0).ToString("yyyy-MM-dd");
                time_end.Text = DateTime.Now.ToString("yyyy-MM-dd");
                //Common.CheckAllCheckbox(cbl_types); cbx_type_all.Checked = true;
                //Common.CheckAllCheckbox(cbl_sites); cbx_site_all.Checked = true;
                //Common.CheckAllCheckbox(cbl_sx); cbx_sx_all.Checked = true;
                //Common.CheckAllCheckbox(cbl_point); cbx_point_all.Checked = true;
                DateTime now = DateTime.Now;
                int hour = now.Hour;
                if (hour >= 0 && hour <= 8)
                {
                    cbl_point.Items[0].Selected = true;
                }
                else if (hour > 9 && hour < 13)
                {
                    cbl_point.Items[1].Selected = true;
                }
                else
                {
                    cbl_point.Items[2].Selected = true;
                }
                cbl_sx.Items[2].Selected = true;
                cbl_sites.Items[2].Selected = true;
                cbl_types.Items[1].Selected = true;
                Search();
            }
        } 
        #endregion

        #region 搜索按钮事件
        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_search_Click(object sender, EventArgs e)
        {
            Search();
        } 
        #endregion


        #region 搜索
        /// <summary>
        /// 搜索
        /// </summary>
        private void Search(int index=1)
        {
            DateTime Time_Begin = DateTime.Parse(time_begin.Text.Trim());
            DateTime Time_End = DateTime.Parse(time_end.Text.Trim());
            
            List<Station> Stations = new List<Station>();
            foreach (ListItem item in cbl_sites.Items)
            {
                if (item.Selected)
                {
                    Stations.Add((Station)Enum.Parse(typeof(Station), item.Value));
                }
            }
            List<Element> Elements = new List<Element>();
            foreach (ListItem item in cbl_types.Items)
            {
                if (item.Selected)
                {
                    Elements.Add((Element)Enum.Parse(typeof(Element), item.Value));
                }
            }

            List<ForeSpanType> FST=new List<ForeSpanType>();
            foreach (ListItem item in cbl_sx.Items)
            {
                if (item.Selected)
                {
                    FST.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), item.Value));
                }
            }

            List<SendPoint> SendPoints = new List<SendPoint>();
            foreach (ListItem item in cbl_point.Items)
            {
                if (item.Selected)
                {
                    SendPoints.Add((SendPoint)Enum.Parse(typeof(SendPoint), item.Value));
                }
            }
            if (Stations.Count == 0)
            {
                anp.Visible = false;
                gv_result.Visible = false;
                div_nodata.Visible = true;
                return;
            }
            anp.CurrentPageIndex = index;
            int pagesize = anp.PageSize;
            int pageindex = anp.CurrentPageIndex;

            var lists = new codeBLL.ForecastBLL().Search(pagesize/Stations.Count, pageindex, type, Time_Begin, Time_End, Elements, FST, Stations,SendPoints,isOptimize);
            gv_result.DataSource = lists;
            gv_result.DataBind();

            anp.RecordCount = new codeBLL.ForecastBLL().CalculateCount(type, Time_Begin, Time_End, Elements, FST, Stations, SendPoints, isOptimize)*Stations.Count;
            if (anp.RecordCount == 0)
            {
                gv_result.Visible = false;
                anp.Visible = false;
                div_nodata.Visible = true;
            }
            else
            {
                gv_result.Visible = true;
                anp.Visible = true;
                div_nodata.Visible = false;
            }

        } 
        #endregion


        #region 处理全选

        protected void cbx_type_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_types.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }


        protected void cbx_point_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_point.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
            Search();
        }


        protected void cbx_site_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_sites.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }

        protected void cbx_sx_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_sx.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }

        #endregion


        #region 更新全选按钮，并按已选择条件搜索

        protected void cbl_point_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_point.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_point_all.Checked = false;
            }
        }

        protected void cbl_types_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_site_all.Checked = false;
            }
        }

        protected void cbl_sites_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_site_all.Checked = false;
            }
        } 
        

        

        protected void cbl_sx_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_sx_all.Checked = false;
            }
        }

#endregion

        #region 处理翻页事件
        /// <summary>
        /// 处理翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            Search(anp.CurrentPageIndex);
        } 
        #endregion

        protected void time_begin_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        protected void time_end_TextChanged(object sender, EventArgs e)
        {
            Search();
        }


        protected void btn_add_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Online.aspx");
            
        }
    }
}